﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;

namespace PdfLib
{
    /// <summary>
    /// 
    /// </summary>
    public class PdfPathDetector
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pdfFilePath"></param>
        /// <param name="pageNum"></param>
        /// <returns></returns>
        public static List<float> GetAllVerticalLines(string pdfFilePath, int pageNum)
        {
            List<float> positions = new List<float>();
            PDFPARSERLib.Document doc = new PDFPARSERLib.Document();
            PDFPARSERLib.Content content = new PDFPARSERLib.Content();
            //PDFPARSERLib.Font font = null;
            try
            {
                if (doc.Open(pdfFilePath, ""))
                {
                    //font = doc.GetFirstFontResource();
                    //while (font != null)
                    //{
                    //    font = doc.GetNextFontResource();
                    //}
                    doc.PageNo = pageNum;
                    PDFPARSERLib.Page page = doc.Page;
                    PDFPARSERLib.Document pageDoc = page.Document;
                    content = pageDoc.Page.Content;
                    System.Drawing.PointF lastPos = new System.Drawing.PointF(0.0F, 0.0F);

                    if (content != null)
                    {
                        content.BreakWords = false;
                        System.Text.RegularExpressions.Regex regex;
                        System.Text.RegularExpressions.Match match;
                        do
                        {
                            if (content.GetNextPath() == null)
                            {
                                break;
                            }
                            string pathGroup = content.Path;
                            if (!string.IsNullOrEmpty(pathGroup))
                            {
                                string[] pathes = pathGroup.Trim().Split(new char[] { '\n' });
                                for (int i = 0; i < pathes.Length; i++)
                                {
                                    string path = pathes[i];
                                    if (path.EndsWith(" m"))
                                    {
                                        regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+m");
                                        match = regex.Match(path);
                                        if (match.Success)
                                        {
                                            float x = float.Parse(match.Groups[1].Value);
                                            float y = float.Parse(match.Groups[2].Value);
                                            y = PdfPathDetector.PdfHeight(page, y);
                                            lastPos = new System.Drawing.PointF(x, y);
                                        }
                                    }
                                    else if (path.EndsWith(" l"))
                                    {
                                        regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+l");
                                        match = regex.Match(path);
                                        if (match.Success)
                                        {
                                            float x = float.Parse(match.Groups[1].Value);
                                            float y = float.Parse(match.Groups[2].Value);
                                            y = PdfPathDetector.PdfHeight(page, y);
                                            if (Math.Abs(x - lastPos.X) < 1)
                                            {
                                                // vertical line 
                                                {
                                                    bool existFlag = false;
                                                    if (positions.Count > 0)
                                                    {
                                                        for (int k = 0; k < positions.Count; k++)
                                                        {
                                                            if (Math.Abs(positions[k] - x) < 1)
                                                            {
                                                                existFlag = true;
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    if (!existFlag)
                                                    {
                                                        positions.Add(x);
                                                    }
                                                }
                                            }
                                            //this.DrawLine(g, lastPos, new System.Drawing.PointF(x, y), pen);
                                            //lastPos = new System.Drawing.PointF(x, y);
                                        }
                                    }
                                    else if (path.EndsWith(" re"))
                                    {
                                        regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+re");
                                        match = regex.Match(path);
                                        if (match.Success)
                                        {
                                            float x = float.Parse(match.Groups[1].Value);
                                            float y = float.Parse(match.Groups[2].Value);
                                            float w = float.Parse(match.Groups[3].Value);
                                            float h = float.Parse(match.Groups[4].Value);
                                            y = PdfPathDetector.PdfHeight(page, y);
                                            if (h < 0)
                                                h = 0 - h;

                                            {
                                                bool existFlag = false;
                                                if (positions.Count > 0)
                                                {
                                                    for (int k = 0; k < positions.Count; k++)
                                                    {
                                                        if (Math.Abs(positions[k] - x) < 1)
                                                        {
                                                            existFlag = true;
                                                            break;
                                                        }
                                                    }
                                                }
                                                if (!existFlag)
                                                {
                                                    positions.Add(x);
                                                }
                                                x = x + w;
                                                existFlag = false;
                                                if (positions.Count > 0)
                                                {
                                                    for (int k = 0; k < positions.Count; k++)
                                                    {
                                                        if (Math.Abs(positions[k] - x) < 1)
                                                        {
                                                            existFlag = true;
                                                            break;
                                                        }
                                                    }
                                                }
                                                if (!existFlag)
                                                {
                                                    positions.Add(x);
                                                }
                                            }
                                            //System.Drawing.RectangleF rect = new System.Drawing.RectangleF(x, y, w, h);
                                            // this.DrawRectangle(g, rect, pen);
                                        }
                                    }
                                    else if (path == "f")
                                    {
                                        if (i > 0)
                                        {
                                            string lastPath = pathes[i - 1];
                                            if (lastPath.EndsWith(" l"))
                                            {
                                                regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+l");
                                                match = regex.Match(lastPath);
                                                if (match.Success)
                                                {
                                                    //float x = float.Parse(match.Groups[1].Value);
                                                    float y = float.Parse(match.Groups[2].Value);
                                                    y = PdfPathDetector.PdfHeight(page, y);
                                                    // lastPos = new System.Drawing.PointF(x, y);
                                                }
                                            }
                                            else if (lastPath.EndsWith(" re"))
                                            {
                                                regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+re");
                                                match = regex.Match(lastPath);
                                                if (match.Success)
                                                {
                                                    //float x = float.Parse(match.Groups[1].Value);
                                                    float y = float.Parse(match.Groups[2].Value);
                                                    //float w = float.Parse(match.Groups[3].Value);
                                                    float h = float.Parse(match.Groups[4].Value);
                                                    y = PdfPathDetector.PdfHeight(page, y);
                                                    if (h < 0)
                                                        h = 0 - h;
                                                    // lastPos = new System.Drawing.PointF(x, y);
                                                }
                                            }
                                        }
                                    }
                                    else if (path == "s")
                                    {
                                    }
                                }
                            }

                        } while (true);
                    }
                }
            }
            catch (Exception ex)
            {
                Common.Utilities.ExceptionHandler.HandlesException(ex);
                GC.Collect();
            }
            finally
            {
                doc.Close();
            }

            return positions;
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pdfFilePath"></param>
        /// <param name="pageNum"></param>
        /// <param name="yRangeFrom"></param>
        /// <param name="yRangeTo"></param>
        /// <returns></returns>
        public static List<float> GetVerticalLines(string pdfFilePath, int pageNum, float yRangeFrom, float yRangeTo)
        {
            List<float> positions = new List<float>();
            PDFPARSERLib.Document doc = new PDFPARSERLib.Document();
            PDFPARSERLib.Content content = new PDFPARSERLib.Content();
            //PDFPARSERLib.Font font = null;
            try
            {
                if (doc.Open(pdfFilePath, ""))
                {
                    //font = doc.GetFirstFontResource();
                    //while (font != null)
                    //{
                    //    font = doc.GetNextFontResource();
                    //}
                    doc.PageNo = pageNum;
                    PDFPARSERLib.Page page = doc.Page;
                    PDFPARSERLib.Document pageDoc = page.Document;
                    content = pageDoc.Page.Content;
                    System.Drawing.PointF lastPos = new System.Drawing.PointF(0.0F, 0.0F);

                    if (content != null)
                    {
                        content.BreakWords = false;
                        System.Text.RegularExpressions.Regex regex;
                        System.Text.RegularExpressions.Match match;
                        do
                        {
                            if (content.GetNextPath() == null)
                            {
                                break;
                            }
                            string pathGroup = content.Path;
                            if (!string.IsNullOrEmpty(pathGroup))
                            {
                                string[] pathes = pathGroup.Trim().Split(new char[] { '\n' });
                                for (int i = 0; i < pathes.Length; i++)
                                {
                                    string path = pathes[i];
                                    if (path.EndsWith(" m"))
                                    {
                                        regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+m");
                                        match = regex.Match(path);
                                        if (match.Success)
                                        {
                                            float x = float.Parse(match.Groups[1].Value);
                                            float y = float.Parse(match.Groups[2].Value);
                                            y = PdfPathDetector.PdfHeight(page, y);
                                            lastPos = new System.Drawing.PointF(x, y);
                                        }
                                    }
                                    else if (path.EndsWith(" l"))
                                    {
                                        regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+l");
                                        match = regex.Match(path);
                                        if (match.Success)
                                        {
                                            float x = float.Parse(match.Groups[1].Value);
                                            float y = float.Parse(match.Groups[2].Value);
                                            y = PdfPathDetector.PdfHeight(page, y);
                                            if (Math.Abs(x - lastPos.X) < 1)
                                            {
                                                // vertical line 
                                                if (y >= yRangeFrom && y <= yRangeTo)
                                                {
                                                    bool existFlag = false;
                                                    if (positions.Count > 0)
                                                    {
                                                        for (int k = 0; k < positions.Count; k++)
                                                        {
                                                            if (Math.Abs(positions[k] - x) < 1)
                                                            {
                                                                existFlag = true;
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    if (!existFlag)
                                                    {
                                                        positions.Add(x);
                                                    }
                                                }
                                            }
                                            //this.DrawLine(g, lastPos, new System.Drawing.PointF(x, y), pen);
                                            //lastPos = new System.Drawing.PointF(x, y);
                                        }
                                    }
                                    else if (path.EndsWith(" re"))
                                    {
                                        regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+re");
                                        match = regex.Match(path);
                                        if (match.Success)
                                        {
                                            float x = float.Parse(match.Groups[1].Value);
                                            float y = float.Parse(match.Groups[2].Value);
                                            float w = float.Parse(match.Groups[3].Value);
                                            float h = float.Parse(match.Groups[4].Value);
                                            y = PdfPathDetector.PdfHeight(page, y);
                                            if (h < 0)
                                                h = 0 - h;

                                            if (y <= yRangeTo && y + h >= yRangeFrom)
                                            {
                                                bool existFlag = false;
                                                if (positions.Count > 0)
                                                {
                                                    for (int k = 0; k < positions.Count; k++)
                                                    {
                                                        if (Math.Abs(positions[k] - x) < 1)
                                                        {
                                                            existFlag = true;
                                                            break;
                                                        }
                                                    }
                                                }
                                                if (!existFlag)
                                                {
                                                    positions.Add(x);
                                                }
                                                x = x + w;
                                                existFlag = false;
                                                if (positions.Count > 0)
                                                {
                                                    for (int k = 0; k < positions.Count; k++)
                                                    {
                                                        if (Math.Abs(positions[k] - x) < 1)
                                                        {
                                                            existFlag = true;
                                                            break;
                                                        }
                                                    }
                                                }
                                                if (!existFlag)
                                                {
                                                    positions.Add(x);
                                                }
                                            }
                                            //System.Drawing.RectangleF rect = new System.Drawing.RectangleF(x, y, w, h);
                                            // this.DrawRectangle(g, rect, pen);
                                        }
                                    }
                                    else if (path == "f")
                                    {
                                        if (i > 0)
                                        {
                                            string lastPath = pathes[i - 1];
                                            if (lastPath.EndsWith(" l"))
                                            {
                                                regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+l");
                                                match = regex.Match(lastPath);
                                                if (match.Success)
                                                {
                                                    //float x = float.Parse(match.Groups[1].Value);
                                                    float y = float.Parse(match.Groups[2].Value);
                                                    y = PdfPathDetector.PdfHeight(page, y);
                                                    // lastPos = new System.Drawing.PointF(x, y);
                                                }
                                            }
                                            else if (lastPath.EndsWith(" re"))
                                            {
                                                regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+re");
                                                match = regex.Match(lastPath);
                                                if (match.Success)
                                                {
                                                    //float x = float.Parse(match.Groups[1].Value);
                                                    float y = float.Parse(match.Groups[2].Value);
                                                    //float w = float.Parse(match.Groups[3].Value);
                                                    float h = float.Parse(match.Groups[4].Value);
                                                    y = PdfPathDetector.PdfHeight(page, y);
                                                    if (h < 0)
                                                        h = 0 - h;
                                                    // lastPos = new System.Drawing.PointF(x, y);
                                                }
                                            }
                                        }
                                    }
                                    else if (path == "s")
                                    {
                                    }
                                }
                            }

                        } while (true);
                    }
                }
            }
            catch (Exception ex)
            {
                Common.Utilities.ExceptionHandler.HandlesException(ex);
                GC.Collect();
            }
            finally
            {
                doc.Close();
            }

            return positions;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pdfFilePath"></param>
        /// <param name="pageNum"></param>
        /// <returns></returns>
        public static List<float> GetAllHorizontalLines(string pdfFilePath, int pageNum)
        {
            List<float> positions = new List<float>();
            PDFPARSERLib.Document doc = new PDFPARSERLib.Document();
            PDFPARSERLib.Content content = new PDFPARSERLib.Content();
            //System.Text.StringBuilder buffer = new StringBuilder();
            try
            {
                if (doc.Open(pdfFilePath, ""))
                {
                    PDFPARSERLib.Font font = doc.GetFirstFontResource();
                    while (font != null)
                    {
                        font = doc.GetNextFontResource();
                    }
                    doc.PageNo = pageNum;
                    PDFPARSERLib.Page page = doc.Page;
                    PDFPARSERLib.Document pageDoc = page.Document;
                    content = pageDoc.Page.Content;
                    System.Drawing.PointF lastPos = new System.Drawing.PointF(0.0F, 0.0F);

                    if (content != null)
                    {
                        content.BreakWords = false;
                        System.Text.RegularExpressions.Regex regex;
                        System.Text.RegularExpressions.Match match;
                        do
                        {
                            if (content.GetNextPath() == null)
                            {
                                break;
                            }
                            string pathGroup = content.Path;
                            if (!string.IsNullOrEmpty(pathGroup))
                            {
                                string[] pathes = pathGroup.Trim().Split(new char[] { '\n' });
                                for (int i = 0; i < pathes.Length; i++)
                                {
                                    string path = pathes[i];
                                    if (path.EndsWith(" m"))
                                    {
                                        regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+m");
                                        match = regex.Match(path);
                                        if (match.Success)
                                        {
                                            float x = float.Parse(match.Groups[1].Value);
                                            float y = float.Parse(match.Groups[2].Value);
                                            y = PdfPathDetector.PdfHeight(page, y);
                                            lastPos = new System.Drawing.PointF(x, y);
                                        }
                                    }
                                    else if (path.EndsWith(" l"))
                                    {
                                        regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+l");
                                        match = regex.Match(path);
                                        if (match.Success)
                                        {
                                            //float x = float.Parse(match.Groups[1].Value);
                                            float y = float.Parse(match.Groups[2].Value);
                                            y = PdfPathDetector.PdfHeight(page, y);
                                            if (Math.Abs(y - lastPos.Y) < 1)
                                            {
                                                // horizontal line 
                                                {
                                                    bool existFlag = false;
                                                    if (positions.Count > 0)
                                                    {
                                                        for (int k = 0; k < positions.Count; k++)
                                                        {
                                                            if (Math.Abs(positions[k] - y) < 1)
                                                            {
                                                                existFlag = true;
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    if (!existFlag)
                                                    {
                                                        positions.Add(y);
                                                    }
                                                }
                                            }
                                            //this.DrawLine(g, lastPos, new System.Drawing.PointF(x, y), pen);
                                            //lastPos = new System.Drawing.PointF(x, y);
                                        }
                                    }
                                    else if (path.EndsWith(" re"))
                                    {
                                        regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+re");
                                        match = regex.Match(path);
                                        if (match.Success)
                                        {
                                            float x = float.Parse(match.Groups[1].Value);
                                            float y = float.Parse(match.Groups[2].Value);
                                            float w = float.Parse(match.Groups[3].Value);
                                            float h = float.Parse(match.Groups[4].Value);
                                            y = PdfPathDetector.PdfHeight(page, y);
                                            if (h < 0)
                                                h = 0 - h;

                                            {
                                                bool existFlag = false;
                                                if (positions.Count > 0)
                                                {
                                                    for (int k = 0; k < positions.Count; k++)
                                                    {
                                                        if (Math.Abs(positions[k] - y) < 1)
                                                        {
                                                            existFlag = true;
                                                            break;
                                                        }
                                                    }
                                                }
                                                if (!existFlag)
                                                {
                                                    positions.Add(y);
                                                }
                                                x = x + w;
                                                existFlag = false;
                                                if (positions.Count > 0)
                                                {
                                                    for (int k = 0; k < positions.Count; k++)
                                                    {
                                                        if (Math.Abs(positions[k] - x) < 1)
                                                        {
                                                            existFlag = true;
                                                            break;
                                                        }
                                                    }
                                                }
                                                if (!existFlag)
                                                {
                                                    positions.Add(y);
                                                }
                                            }
                                            //System.Drawing.RectangleF rect = new System.Drawing.RectangleF(x, y, w, h);
                                            // this.DrawRectangle(g, rect, pen);
                                        }
                                    }
                                    else if (path == "f")
                                    {
                                        if (i > 0)
                                        {
                                            string lastPath = pathes[i - 1];
                                            if (lastPath.EndsWith(" l"))
                                            {
                                                regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+l");
                                                match = regex.Match(lastPath);
                                                if (match.Success)
                                                {
                                                    //float x = float.Parse(match.Groups[1].Value);
                                                    float y = float.Parse(match.Groups[2].Value);
                                                    y = PdfPathDetector.PdfHeight(page, y);
                                                    // lastPos = new System.Drawing.PointF(x, y);
                                                }
                                            }
                                            else if (lastPath.EndsWith(" re"))
                                            {
                                                regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+re");
                                                match = regex.Match(lastPath);
                                                if (match.Success)
                                                {
                                                    //float x = float.Parse(match.Groups[1].Value);
                                                    float y = float.Parse(match.Groups[2].Value);
                                                    //float w = float.Parse(match.Groups[3].Value);
                                                    float h = float.Parse(match.Groups[4].Value);
                                                    y = PdfPathDetector.PdfHeight(page, y);
                                                    if (h < 0)
                                                        h = 0 - h;
                                                    // lastPos = new System.Drawing.PointF(x, y);
                                                }
                                            }
                                        }
                                    }
                                    else if (path == "s")
                                    {
                                    }
                                }
                            }

                        } while (true);
                    }
                }
            }
            catch (Exception ex)
            {
                Common.Utilities.ExceptionHandler.HandlesException(ex);
                GC.Collect();
            }
            finally
            {
                if (doc != null)
                {
                    doc.Close();
                }
            }

            return positions;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pdfFilePath"></param>
        /// <param name="pageNum"></param>
        /// <param name="xRangeFrom"></param>
        /// <param name="xRangeTo"></param>
        /// <returns></returns>
        public static List<float> GetHorizontalLines(string pdfFilePath, int pageNum, float xRangeFrom, float xRangeTo)
        {
            List<float> positions = new List<float>();
            PDFPARSERLib.Document doc = new PDFPARSERLib.Document();
            PDFPARSERLib.Content content = new PDFPARSERLib.Content();
            //System.Text.StringBuilder buffer = new StringBuilder();
            try
            {
                if (doc.Open(pdfFilePath, ""))
                {
                    PDFPARSERLib.Font font = doc.GetFirstFontResource();
                    while (font != null)
                    {
                        font = doc.GetNextFontResource();
                    }
                    doc.PageNo = pageNum;
                    PDFPARSERLib.Page page = doc.Page;
                    PDFPARSERLib.Document pageDoc = page.Document;
                    content = pageDoc.Page.Content;
                    System.Drawing.PointF lastPos = new System.Drawing.PointF(0.0F, 0.0F);

                    if (content != null)
                    {
                        content.BreakWords = false;
                        System.Text.RegularExpressions.Regex regex;
                        System.Text.RegularExpressions.Match match;
                        do
                        {
                            if (content.GetNextPath() == null)
                            {
                                break;
                            }
                            string pathGroup = content.Path;
                            if (!string.IsNullOrEmpty(pathGroup))
                            {
                                string[] pathes = pathGroup.Trim().Split(new char[] { '\n' });
                                for (int i = 0; i < pathes.Length; i++)
                                {
                                    string path = pathes[i];
                                    if (path.EndsWith(" m"))
                                    {
                                        regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+m");
                                        match = regex.Match(path);
                                        if (match.Success)
                                        {
                                            float x = float.Parse(match.Groups[1].Value);
                                            float y = float.Parse(match.Groups[2].Value);
                                            y = PdfPathDetector.PdfHeight(page, y);
                                            lastPos = new System.Drawing.PointF(x, y);
                                        }
                                    }
                                    else if (path.EndsWith(" l"))
                                    {
                                        regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+l");
                                        match = regex.Match(path);
                                        if (match.Success)
                                        {
                                            float x = float.Parse(match.Groups[1].Value);
                                            float y = float.Parse(match.Groups[2].Value);
                                            y = PdfPathDetector.PdfHeight(page, y);
                                            if (Math.Abs(y - lastPos.Y) < 1)
                                            {
                                                // horizontal line 
                                                if (x >= xRangeFrom && x <= xRangeTo)
                                                {
                                                    bool existFlag = false;
                                                    if (positions.Count > 0)
                                                    {
                                                        for (int k = 0; k < positions.Count; k++)
                                                        {
                                                            if (Math.Abs(positions[k] - y) < 1)
                                                            {
                                                                existFlag = true;
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    if (!existFlag)
                                                    {
                                                        positions.Add(y);
                                                    }
                                                }
                                            }
                                            //this.DrawLine(g, lastPos, new System.Drawing.PointF(x, y), pen);
                                            //lastPos = new System.Drawing.PointF(x, y);
                                        }
                                    }
                                    else if (path.EndsWith(" re"))
                                    {
                                        regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+re");
                                        match = regex.Match(path);
                                        if (match.Success)
                                        {
                                            float x = float.Parse(match.Groups[1].Value);
                                            float y = float.Parse(match.Groups[2].Value);
                                            float w = float.Parse(match.Groups[3].Value);
                                            float h = float.Parse(match.Groups[4].Value);
                                            y = PdfPathDetector.PdfHeight(page, y);
                                            if (h < 0)
                                                h = 0 - h;

                                            if (x <= xRangeTo && x + h >= xRangeFrom)
                                            {
                                                bool existFlag = false;
                                                if (positions.Count > 0)
                                                {
                                                    for (int k = 0; k < positions.Count; k++)
                                                    {
                                                        if (Math.Abs(positions[k] - y) < 1)
                                                        {
                                                            existFlag = true;
                                                            break;
                                                        }
                                                    }
                                                }
                                                if (!existFlag)
                                                {
                                                    positions.Add(y);
                                                }
                                                x = x + w;
                                                existFlag = false;
                                                if (positions.Count > 0)
                                                {
                                                    for (int k = 0; k < positions.Count; k++)
                                                    {
                                                        if (Math.Abs(positions[k] - x) < 1)
                                                        {
                                                            existFlag = true;
                                                            break;
                                                        }
                                                    }
                                                }
                                                if (!existFlag)
                                                {
                                                    positions.Add(y);
                                                }
                                            }
                                            //System.Drawing.RectangleF rect = new System.Drawing.RectangleF(x, y, w, h);
                                            // this.DrawRectangle(g, rect, pen);
                                        }
                                    }
                                    else if (path == "f")
                                    {
                                        if (i > 0)
                                        {
                                            string lastPath = pathes[i - 1];
                                            if (lastPath.EndsWith(" l"))
                                            {
                                                regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+l");
                                                match = regex.Match(lastPath);
                                                if (match.Success)
                                                {
                                                    //float x = float.Parse(match.Groups[1].Value);
                                                    float y = float.Parse(match.Groups[2].Value);
                                                    y = PdfPathDetector.PdfHeight(page, y);
                                                    // lastPos = new System.Drawing.PointF(x, y);
                                                }
                                            }
                                            else if (lastPath.EndsWith(" re"))
                                            {
                                                regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+re");
                                                match = regex.Match(lastPath);
                                                if (match.Success)
                                                {
                                                    //float x = float.Parse(match.Groups[1].Value);
                                                    float y = float.Parse(match.Groups[2].Value);
                                                    //float w = float.Parse(match.Groups[3].Value);
                                                    float h = float.Parse(match.Groups[4].Value);
                                                    y = PdfPathDetector.PdfHeight(page, y);
                                                    if (h < 0)
                                                        h = 0 - h;
                                                    // lastPos = new System.Drawing.PointF(x, y);
                                                }
                                            }
                                        }
                                    }
                                    else if (path == "s")
                                    {
                                    }
                                }
                            }

                        } while (true);
                    }
                }
            }
            catch (Exception ex)
            {
                Common.Utilities.ExceptionHandler.HandlesException(ex);
                GC.Collect();
            }
            finally
            {
                if (doc != null)
                {
                    doc.Close();
                }
            }

            return positions;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pdfFilePath"></param>
        /// <param name="pageNum"></param>
        /// <returns></returns>
        public static List<string> GetPdfPathAsString(string pdfFilePath, int pageNum)
        {
            List<string> paths = new List<string>();
            PDFPARSERLib.Document doc = new PDFPARSERLib.Document();
            PDFPARSERLib.Content content = new PDFPARSERLib.Content();
            //System.Text.StringBuilder buffer = new StringBuilder();
            try
            {
                if (doc.Open(pdfFilePath, ""))
                {
                    PDFPARSERLib.Font font = doc.GetFirstFontResource();
                    while (font != null)
                    {
                        font = doc.GetNextFontResource();
                    }
                    doc.PageNo = pageNum;
                    PDFPARSERLib.Page page = doc.Page;
                    PDFPARSERLib.Document pageDoc = page.Document;
                    content = pageDoc.Page.Content;
                    //System.Drawing.PointF lastPos = new System.Drawing.PointF(0.0F, 0.0F);

                    if (content != null)
                    {
                        content.BreakWords = false;
                        do
                        {
                            if (content.GetNextPath() == null)
                            {
                                break;
                            }
                            string pathGroup = content.Path;
                            if (!string.IsNullOrEmpty(pathGroup))
                            {
                                pathGroup = pathGroup.Trim();
                                paths.Add(pathGroup);
                            }

                        } while (true);
                    }
                }
            }
            catch (Exception ex)
            {
                Common.Utilities.ExceptionHandler.HandlesException(ex);
                GC.Collect();
            }
            finally
            {
                if (doc != null)
                {
                    doc.Close();
                }
            }
            return paths;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pdfFilePath"></param>
        /// <param name="pageNum"></param>
        /// <returns></returns>
        public static System.Drawing.Bitmap GetPdfPathAsImage(string pdfFilePath, int pageNum)
        {
            PDFPARSERLib.Document doc = new PDFPARSERLib.Document();
            PDFPARSERLib.Content content = new PDFPARSERLib.Content();
            //System.Text.StringBuilder buffer = new StringBuilder();
            System.Drawing.Bitmap img = new System.Drawing.Bitmap(1000, 1000);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
            try
            {
                if (doc.Open(pdfFilePath, ""))
                {
                    PDFPARSERLib.Font font = doc.GetFirstFontResource();
                    while (font != null)
                    {
                        font = doc.GetNextFontResource();
                    }
                    doc.PageNo = pageNum;
                    PDFPARSERLib.Page page = doc.Page;
                    PDFPARSERLib.Document pageDoc = page.Document;
                    content = pageDoc.Page.Content;
                    System.Drawing.PointF lastPos = new System.Drawing.PointF(0.0F, 0.0F);

                    if (content != null)
                    {
                        content.BreakWords = false;
                        System.Text.RegularExpressions.Regex regex;
                        System.Text.RegularExpressions.Match match;
                        do
                        {
                            if (content.GetNextPath() == null)
                            {
                                break;
                            }
                            string pathGroup = content.Path;
                            if (!string.IsNullOrEmpty(pathGroup))
                            {
                                string[] pathes = pathGroup.Trim().Split(new char[] { '\n' });
                                for (int i = 0; i < pathes.Length; i++)
                                {
                                    string path = pathes[i];
                                    if (path.EndsWith(" m"))
                                    {
                                        regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+m");
                                        match = regex.Match(path);
                                        if (match.Success)
                                        {
                                            float x = float.Parse(match.Groups[1].Value);
                                            float y = float.Parse(match.Groups[2].Value);
                                            y = PdfPathDetector.PdfHeight(page, y);
                                            lastPos = new System.Drawing.PointF(x, y);
                                        }
                                    }
                                    else if (path.EndsWith(" l"))
                                    {
                                        regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+l");
                                        match = regex.Match(path);
                                        if (match.Success)
                                        {
                                            float x = float.Parse(match.Groups[1].Value);
                                            float y = float.Parse(match.Groups[2].Value);
                                            y = PdfPathDetector.PdfHeight(page, y);
                                            PdfPathDetector.DrawLine(g, lastPos, new System.Drawing.PointF(x, y), pen);
                                            //lastPos = new System.Drawing.PointF(x, y);
                                        }
                                    }
                                    else if (path.EndsWith(" re"))
                                    {
                                        regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+re");
                                        match = regex.Match(path);
                                        if (match.Success)
                                        {
                                            float x = float.Parse(match.Groups[1].Value);
                                            float y = float.Parse(match.Groups[2].Value);
                                            float w = float.Parse(match.Groups[3].Value);
                                            float h = float.Parse(match.Groups[4].Value);
                                            y = PdfPathDetector.PdfHeight(page, y);
                                            if (h < 0)
                                                h = 0 - h;
                                            System.Drawing.RectangleF rect = new System.Drawing.RectangleF(x, y, w, h);
                                            PdfPathDetector.DrawRectangle(g, rect, pen);
                                        }
                                    }
                                    else if (path == "f")
                                    {
                                        if (i > 0)
                                        {
                                            string lastPath = pathes[0];
                                            if (lastPath.EndsWith(" l"))
                                            {
                                                regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+l");
                                                match = regex.Match(lastPath);
                                                if (match.Success)
                                                {
                                                    float x = float.Parse(match.Groups[1].Value);
                                                    float y = float.Parse(match.Groups[2].Value);
                                                    y = PdfPathDetector.PdfHeight(page, y);
                                                    PdfPathDetector.DrawLine(g, lastPos, new PointF(x, y), pen);
                                                    // lastPos = new System.Drawing.PointF(x, y);
                                                }
                                            }
                                            else if (lastPath.EndsWith(" re"))
                                            {
                                                regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+re");
                                                match = regex.Match(lastPath);
                                                if (match.Success)
                                                {
                                                    float x = float.Parse(match.Groups[1].Value);
                                                    float y = float.Parse(match.Groups[2].Value);
                                                    float w = float.Parse(match.Groups[3].Value);
                                                    float h = float.Parse(match.Groups[4].Value);
                                                    y = PdfPathDetector.PdfHeight(page, y);
                                                    if (h < 0)
                                                        h = 0 - h;
                                                    PdfPathDetector.DrawRectangle(g, new RectangleF(x, y, w, h), pen);
                                                    //lastPos = new System.Drawing.PointF(x, y);
                                                }
                                            }
                                        }
                                    }
                                    else if (path == "s")
                                    {
                                    }
                                }
                            }

                        } while (true);
                    }
                }
            }
            catch (Exception ex)
            {
                Common.Utilities.ExceptionHandler.HandlesException(ex);
                GC.Collect();
            }
            finally
            {
                if (doc != null)
                {
                    doc.Close();
                }
            }

            pen.Dispose();
            g.Dispose();
            return img;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="lastPosition"></param>
        /// <param name="pathToken"></param>
        /// <returns></returns>
        public static HorizontalLine GetHorizontalLine(ref PDFPARSERLib.Page page, ref PointF lastPosition, string pathToken)
        {
            HorizontalLine hLine = new HorizontalLine(0, 0, 0);
            hLine.X1 = 0f;
            hLine.X2 = 0f;
            hLine.Y = 0f;

            Single minLength = 1f;
            System.Text.RegularExpressions.Regex regex;
            System.Text.RegularExpressions.Match match;
            string[] pathes = pathToken.Trim().Split(new char[] { '\n' });
            for (int i = 0; i < pathes.Length; i++)
            {
                string path = pathes[i];
                if (path.EndsWith(" m"))
                {
                    regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+m");
                    match = regex.Match(path);
                    if (match.Success)
                    {
                        float x = float.Parse(match.Groups[1].Value);
                        float y = float.Parse(match.Groups[2].Value);
                        y = PdfPathDetector.PdfHeight(page, y);
                        lastPosition = new System.Drawing.PointF(x, y);
                    }
                }
                else if (path.EndsWith(" l"))
                {
                    regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+l");
                    match = regex.Match(path);
                    if (match.Success)
                    {
                        float x = float.Parse(match.Groups[1].Value);
                        float y = float.Parse(match.Groups[2].Value);
                        y = PdfPathDetector.PdfHeight(page, y);
                        if (Math.Abs(x - lastPosition.X) > minLength && Math.Abs(y - lastPosition.Y) < minLength)
                        {
                            hLine.X1 = Math.Min(lastPosition.X, x);
                            hLine.X2 = Math.Max(lastPosition.X, x);
                            hLine.Y = y;
                        }
                    }
                }
                else if (path.EndsWith(" re"))
                {
                    regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+re");
                    match = regex.Match(path);
                    if (match.Success)
                    {
                        float x = float.Parse(match.Groups[1].Value);
                        float y = float.Parse(match.Groups[2].Value);
                        float w = float.Parse(match.Groups[3].Value);
                        float h = float.Parse(match.Groups[4].Value);
                        y = PdfPathDetector.PdfHeight(page, y);
                        if (h < 0)
                            h = 0 - h;
                        hLine.X1 = x;
                        hLine.X2 = x + w;
                        hLine.Y = y + h;
                    }
                }
                else if (path == "f")
                {
                    if (i > 0)
                    {
                        string lastPath = pathes[0];
                        if (lastPath.EndsWith(" l"))
                        {
                            regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+l");
                            match = regex.Match(lastPath);
                            if (match.Success)
                            {
                                float x = float.Parse(match.Groups[1].Value);
                                float y = float.Parse(match.Groups[2].Value);
                                y = PdfPathDetector.PdfHeight(page, y);
                                if (Math.Abs(x - lastPosition.X) > minLength && Math.Abs(y - lastPosition.Y) < minLength)
                                {
                                    hLine.X1 = Math.Min(lastPosition.X, x);
                                    hLine.X2 = Math.Max(lastPosition.X, x);
                                    hLine.Y = y;
                                }
                            }
                        }
                        else if (lastPath.EndsWith(" re"))
                        {
                            regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+re");
                            match = regex.Match(lastPath);
                            if (match.Success)
                            {
                                float x = float.Parse(match.Groups[1].Value);
                                float y = float.Parse(match.Groups[2].Value);
                                float w = float.Parse(match.Groups[3].Value);
                                float h = float.Parse(match.Groups[4].Value);
                                y = PdfPathDetector.PdfHeight(page, y);
                                if (h < 0)
                                    h = 0 - h;
                                hLine.X1 = x;
                                hLine.X2 = x + w;
                                hLine.Y = y + h;
                            }
                        }
                    }
                }
                else if (path == "s")
                {
                }
            }

            return hLine;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="lastPosition"></param>
        /// <param name="pathToken"></param>
        /// <returns></returns>
        public static VerticalLine GetVerticalLine(ref PDFPARSERLib.Page page, ref PointF lastPosition, string pathToken)
        {
            VerticalLine vLine = new VerticalLine(0, 0, 0);
            vLine.X = 0f;
            vLine.Y1 = 0f;
            vLine.Y2 = 0f;

            Single minLength = 1f;
            System.Text.RegularExpressions.Regex regex;
            System.Text.RegularExpressions.Match match;
            string[] pathes = pathToken.Trim().Split(new char[] { '\n' });
            for (int i = 0; i < pathes.Length; i++)
            {
                string path = pathes[i];
                if (path.EndsWith(" m"))
                {
                    regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+m");
                    match = regex.Match(path);
                    if (match.Success)
                    {
                        float x = float.Parse(match.Groups[1].Value);
                        float y = float.Parse(match.Groups[2].Value);
                        y = PdfPathDetector.PdfHeight(page, y);
                        lastPosition = new System.Drawing.PointF(x, y);
                    }
                }
                else if (path.EndsWith(" l"))
                {
                    regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+l");
                    match = regex.Match(path);
                    if (match.Success)
                    {
                        float x = float.Parse(match.Groups[1].Value);
                        float y = float.Parse(match.Groups[2].Value);
                        y = PdfPathDetector.PdfHeight(page, y);
                        if (Math.Abs(x - lastPosition.X) < minLength && Math.Abs(y - lastPosition.Y) > minLength)
                        {
                            vLine.X = x;
                            vLine.Y1 = Math.Min(lastPosition.Y, y);
                            vLine.Y2 = Math.Max(lastPosition.Y, y);
                        }
                    }
                }
                else if (path.EndsWith(" re"))
                {
                    regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+re");
                    match = regex.Match(path);
                    if (match.Success)
                    {
                        float x = float.Parse(match.Groups[1].Value);
                        float y = float.Parse(match.Groups[2].Value);
                        float w = float.Parse(match.Groups[3].Value);
                        float h = float.Parse(match.Groups[4].Value);
                        y = PdfPathDetector.PdfHeight(page, y);
                        if (h < 0)
                            h = 0 - h;
                        vLine.X = x + w;
                        vLine.Y1 = y;
                        vLine.Y2 = y + h;
                    }
                }
                else if (path == "f")
                {
                    if (i > 0)
                    {
                        string lastPath = pathes[0];
                        if (lastPath.EndsWith(" l"))
                        {
                            regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+l");
                            match = regex.Match(lastPath);
                            if (match.Success)
                            {
                                float x = float.Parse(match.Groups[1].Value);
                                float y = float.Parse(match.Groups[2].Value);
                                y = PdfPathDetector.PdfHeight(page, y);
                                if (Math.Abs(x - lastPosition.X) < minLength && Math.Abs(y - lastPosition.Y) > minLength)
                                {
                                    vLine.X = x;
                                    vLine.Y1 = Math.Min(lastPosition.Y, y);
                                    vLine.Y2 = Math.Max(lastPosition.Y, y);
                                }
                            }
                        }
                        else if (lastPath.EndsWith(" re"))
                        {
                            regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+re");
                            match = regex.Match(lastPath);
                            if (match.Success)
                            {
                                float x = float.Parse(match.Groups[1].Value);
                                float y = float.Parse(match.Groups[2].Value);
                                float w = float.Parse(match.Groups[3].Value);
                                float h = float.Parse(match.Groups[4].Value);
                                y = PdfPathDetector.PdfHeight(page, y);
                                if (h < 0)
                                    h = 0 - h;
                                vLine.X = x + w;
                                vLine.Y1 = y;
                                vLine.Y2 = y + h;
                            }
                        }
                    }
                }
                else if (path == "s")
                {
                }
            }

            return vLine;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="lastPosition"></param>
        /// <param name="pathToken"></param>
        public static void UpdateLastPosition(ref PDFPARSERLib.Page page, ref PointF lastPosition, string pathToken)
        {
            System.Text.RegularExpressions.Regex regex;
            System.Text.RegularExpressions.Match match;
            string[] pathes = pathToken.Trim().Split(new char[] { '\n' });
            for (int i = 0; i < pathes.Length; i++)
            {
                string path = pathes[i];
                if (path.EndsWith(" m"))
                {
                    regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+m");
                    match = regex.Match(path);
                    if (match.Success)
                    {
                        float x = float.Parse(match.Groups[1].Value);
                        float y = float.Parse(match.Groups[2].Value);
                        y = PdfPathDetector.PdfHeight(page, y);
                        lastPosition = new System.Drawing.PointF(x, y);
                    }
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pathToken"></param>
        /// <returns></returns>
        public static PdfDrawType GetPathType(string pathToken)
        {
            PdfDrawType pathType = PdfDrawType.Other;
            //Single minLength = 1f;
            System.Text.RegularExpressions.Regex regex;
            System.Text.RegularExpressions.Match match;
            string[] pathes = pathToken.Trim().Split(new char[] { '\n' });
            for (int i = 0; i < pathes.Length; i++)
            {
                string path = pathes[i];
                if (path.EndsWith(" m"))
                {
                    regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+m");
                    match = regex.Match(path);
                    if (match.Success)
                    {
                        pathType = PdfDrawType.Move;
                    }
                }
                else if (path.EndsWith(" l"))
                {
                    regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+l");
                    match = regex.Match(path);
                    if (match.Success)
                    {
                        pathType = PdfDrawType.DrawLine;
                    }
                }
                else if (path.EndsWith(" re"))
                {
                    regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+re");
                    match = regex.Match(path);
                    if (match.Success)
                    {
                        pathType = PdfDrawType.DrawRect;
                    }
                }
                else if (path == "f")
                {
                    if (i > 0)
                    {
                        string lastPath = pathes[0];
                        if (lastPath.EndsWith(" l"))
                        {
                            regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+l");
                            match = regex.Match(lastPath);
                            if (match.Success)
                            {
                                pathType = PdfDrawType.FillLine;
                            }
                        }
                        else if (lastPath.EndsWith(" re"))
                        {
                            regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+re");
                            match = regex.Match(lastPath);
                            if (match.Success)
                            {
                                pathType = PdfDrawType.FillRect;
                            }
                        }
                    }
                }
                else if (path == "s")
                {
                }
            }

            return pathType;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pathToken"></param>
        public static void UpdateRectanglePathToken(ref PDFPARSERLib.Page page, ref string pathToken)
        {
            System.Text.RegularExpressions.Regex regex;
            System.Text.RegularExpressions.Match match;
            string[] pathes = pathToken.Trim().Split(new char[] { '\n' });
            for (int i = 0; i < pathes.Length; i++)
            {
                string path = pathes[i];
                if (path.EndsWith(" re"))
                {
                    regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+re");
                    match = regex.Match(path);
                    if (match.Success)
                    {
                        float x = float.Parse(match.Groups[1].Value);
                        float y = float.Parse(match.Groups[2].Value);
                        float w = float.Parse(match.Groups[3].Value);
                        float h = float.Parse(match.Groups[4].Value);
                        y = PdfPathDetector.PdfHeight(page, y);
                        if (h < 0)
                            h = 0 - h;
                        pathToken = pathToken.Replace(match.Value, x.ToString() + " " + y.ToString() + " " + w.ToString() + " " + h.ToString() + " re");
                    }
                }
                else if (path == "f")
                {
                    if (i > 0)
                    {
                        string lastPath = pathes[0];
                        if (lastPath.EndsWith(" re"))
                        {
                            regex = new System.Text.RegularExpressions.Regex("^(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+(-?\\d+\\.?\\d*)\\s+re");
                            match = regex.Match(lastPath);
                            if (match.Success)
                            {
                                float x = float.Parse(match.Groups[1].Value);
                                float y = float.Parse(match.Groups[2].Value);
                                float w = float.Parse(match.Groups[3].Value);
                                float h = float.Parse(match.Groups[4].Value);
                                y = PdfPathDetector.PdfHeight(page, y);
                                if (h < 0)
                                    h = 0 - h;
                                pathToken = pathToken.Replace(match.Value, x.ToString() + " " + y.ToString() + " " + w.ToString() + " " + h.ToString() + " re");
                            }
                        }
                    }
                }
            }
        }

        #region drawing
        private static void DrawLine(System.Drawing.Graphics g, System.Drawing.PointF from, System.Drawing.PointF to, System.Drawing.Pen p)
        {
            g.DrawLine(p, from, to);
        }

        private static void DrawRectangle(System.Drawing.Graphics g, System.Drawing.RectangleF rect, System.Drawing.Pen p)
        {
            if (rect.Height < 0)
            {
                rect.Y = rect.Y - rect.Height;
                rect.Height = 0 - rect.Height;
            }
            g.DrawRectangle(p, rect.X, rect.Y, rect.Width, rect.Height);
        }
        #endregion

        #region hight
        private static float PdfHeight(PDFPARSERLib.Page page, float height)
        {
            object[] dimensions = (object[])page.CropBox;
            if (dimensions != null && dimensions.Length == 4)
            {
                //float pageWidth = float.Parse(dimensions[2].ToString());
                float pageHeight = float.Parse(dimensions[3].ToString());
                height = pageHeight - height;
            }
            return height;
        }
        #endregion


    }
}
